package com.gnerv.pabu.basic.core.utils;

import com.gnerv.pabu.basic.core.dto.LoginDTO;
import com.google.code.kaptcha.Constants;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class KaptchaUtils {

    /**
     * 验证码校验
     *
     * @param request
     * @return
     */
    public static boolean checkVerifyCode(LoginDTO loginDTO, HttpServletRequest request) {
        if(StringUtils.isEmpty(loginDTO.getKaptcha())){
            return false;
        }
        HttpSession session = request.getSession();
        //获取生成的验证码
        String verifyCodeExpected = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        //获取用户输入的验证码
        if (loginDTO.getKaptcha().equals(verifyCodeExpected)) {
            return true;
        }
        return false;
    }

}
